//
//  AuthorityUtils.m
//  SheMeasured
//
//  Created by ddkj on 2019/5/14.
//  Copyright © 2019 xmddkj. All rights reserved.
//

#import "AuthorityUtils.h"
#import <Photos/Photos.h>
#import <Contacts/Contacts.h>

@implementation AuthorityUtils

+ (void)requestAuthorForCamera:(void(^)(VisitAuthorityType granted))result {
    
    AVAuthorizationStatus cameraStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    if (cameraStatus == AVAuthorizationStatusAuthorized)
    {
        if (result) {
            result(VisitAuthorityTypeAccept);
        }
    }
    else if (cameraStatus == AVAuthorizationStatusNotDetermined)
    {
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (!result) {
                    return;
                }
                if (granted) {
                    result(VisitAuthorityTypeAccept);
                } else {
                    result(VisitAuthorityTypeRefuse);
                }
            });
        }];
    }
    else
    {
        if (result) {
            result(VisitAuthorityTypeRefuse);
        }
    }
}


+ (void)requestAuthorForPhotoLibrary:(void(^)(VisitAuthorityType granted))result {
    
    PHAuthorizationStatus photoStatus = [PHPhotoLibrary authorizationStatus];
    
    if (photoStatus == PHAuthorizationStatusAuthorized)
    {
        if (result) {
            result(VisitAuthorityTypeAccept);
        }
    }
    else if (photoStatus == PHAuthorizationStatusNotDetermined)
    {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            dispatch_async(dispatch_get_main_queue(), ^{
                if (!result) {
                    return;
                }
                if (status == PHAuthorizationStatusAuthorized) {
                    result(VisitAuthorityTypeAccept);
                } else {
                    result(VisitAuthorityTypeRefuse);
                }
            });
        }];
    }
    else
    {
        if (result) {
            result(VisitAuthorityTypeRefuse);
        }
    }
}


+ (void)requestAuthorForContact:(void(^)(VisitAuthorityType granted))result {
    
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    
    if (status == CNAuthorizationStatusAuthorized)
    {
        if (result) {
            result(VisitAuthorityTypeAccept);
        }
    }
    else if (status == CNAuthorizationStatusNotDetermined)
    {
        CNContactStore *store = [[CNContactStore alloc] init];
        [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError*  _Nullable error) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                if (!result) {
                    return;
                }
                
                if (granted) {
                    result(VisitAuthorityTypeAccept);
                } else {
                    result(VisitAuthorityTypeRefuse);
                }
            });
        }];
    }
    else
    {
        if (result) {
            result(VisitAuthorityTypeRefuse);
        }
    }
}

@end
